package club.xiaojiawei.dp;

/**
 * @author 肖嘉威
 * @version 1.0
 * @date 6/1/22 2:37 PM
 * @question 63. 不同路径 II
 * @description 一个机器人位于一个 m x n 网格的左上角 （起始点在下图中标记为 “Start” ）。
 * 机器人每次只能向下或者向右移动一步。机器人试图达到网格的右下角（在下图中标记为 “Finish”）。
 * 现在考虑网格中有障碍物。那么从左上角到右下角将会有多少条不同的路径？
 * 网格中的障碍物和空位置分别用 1 和 0 来表示。
 */
public class UniquePathsWithObstacles63 {

    public static void main(String[] args) {
        UniquePathsWithObstacles63 test = new UniquePathsWithObstacles63();
        int result = test.uniquePathsWithObstacles2(new int[][]{{0, 0, 0}, {0, 1, 0}, {0, 0, 0}});
        System.out.printf("共有%s种方法\n", result);
    }

    /**
     * dp
     * 时间复杂度O(mn)
     * 空间复杂度O(mn)
     * @param obstacleGrid
     * @return
     */
    public int uniquePathsWithObstacles(int[][] obstacleGrid) {
        int height = obstacleGrid.length;
        int width = obstacleGrid[0].length;
        if (obstacleGrid[0][0] == 1 || obstacleGrid[height - 1][width - 1] == 1){
            return 0;
        }
        int[][] maze = new int[height][width];
        for (int i = 0; i < width; i++) {
            if (obstacleGrid[0][i] == 1){
                break;
            }
            maze[0][i] = 1;
        }
        for (int i = 1; i < height; i++) {
            if (obstacleGrid[i][0] == 1){
                break;
            }
            maze[i][0] = 1;
        }
        for (int h = 1; h < height; h++) {
            for (int w = 1; w < width; w++) {
                if (obstacleGrid[h][w] != 1){
                    maze[h][w] = maze[h - 1][w] + maze[h][w - 1];
                }
            }
        }
        return maze[height - 1][width - 1];
    }

    /**
     * 官方-dp(滚动数组)
     * 时间复杂度O(mn)
     * 空间复杂度O(m)
     * @param obstacleGrid
     * @return
     */
    public int uniquePathsWithObstacles2(int[][] obstacleGrid) {
        int m = obstacleGrid[0].length;
        int[] f = new int[m];
        f[0] = obstacleGrid[0][0] == 0 ? 1 : 0;
        for (int[] ints : obstacleGrid) {
            for (int j = 0; j < m; ++j) {
                if (ints[j] == 1) {
                    f[j] = 0;
                    continue;
                }
                if (j - 1 >= 0 && ints[j - 1] == 0) {
                    f[j] += f[j - 1];
                }
            }
        }
        return f[m - 1];
    }

    /**
     * dp-空间复杂度优化
     * 时间复杂度O(mn)
     * 空间复杂度O(1)
     * @param obstacleGrid
     * @return
     */
    public int uniquePathsWithObstacles3(int[][] obstacleGrid) {
        int height = obstacleGrid.length;
        int width = obstacleGrid[0].length;
        if (obstacleGrid[0][0] == 1 || obstacleGrid[height - 1][width - 1] == 1){
            return 0;
        }
        boolean flag = false;
        for (int i = 0; i < width; i++) {
            if (obstacleGrid[0][i] == 1){
                obstacleGrid[0][i] = 0;
                flag = true;
            }else if (!flag){
                obstacleGrid[0][i] = 1;
            }
        }
        flag = false;
        for (int i = 1; i < height; i++) {
            if (obstacleGrid[i][0] == 1){
                obstacleGrid[i][0] = 0;
                flag = true;
            }else if (!flag){
                obstacleGrid[i][0] = 1;
            }
        }
        for (int h = 1; h < height; h++) {
            for (int w = 1; w < width; w++) {
                if (obstacleGrid[h][w] != 1){
                    obstacleGrid[h][w] = obstacleGrid[h - 1][w] + obstacleGrid[h][w - 1];
                }else {
                    obstacleGrid[h][w] = 0;
                }
            }
        }
        return obstacleGrid[height - 1][width - 1];
    }
}
